package com.tool.o2m.model;

import cn.hutool.core.util.StrUtil;

public class TableKey {

    public String tableName;

    /** 键名 */
    public String keyName;

    /** 键类型：主键（PRIMARY）、唯一键（UNIQUE）、普通键 */
    public String keyType;

    /** 列名：如果索引或者主键中包含函数，则列名需要从列表达式中取 */
    public String columnName;

    /** 列表达式：如果索引或者主键中包含函数，则此属性不为空 */
    public String columnExpression;

    /** 列的位置。如果是复合键，则此属性为列的位置，从1开始*/
    public Integer columnPosition;

    /** 键类型排序：主键排第一，然后唯一键排第二，普通键排第三*/
    public Integer keyTypeSort;

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getKeyName() {
        return keyName;
    }

    public void setKeyName(String keyName) {
        this.keyName = keyName;
    }

    public String getKeyType() {
        return keyType;
    }

    public void setKeyType(String keyType) {
        this.keyType = keyType;
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public String getColumnExpression() {
        return columnExpression;
    }

    public void setColumnExpression(String columnExpression) {
        this.columnExpression = columnExpression;
    }

    public Integer getColumnPosition() {
        return columnPosition;
    }

    public void setColumnPosition(Integer columnPosition) {
        this.columnPosition = columnPosition;
    }

    public Integer getKeyTypeSort() {
        return keyTypeSort;
    }

    public void setKeyTypeSort(Integer keyTypeSort) {
        this.keyTypeSort = keyTypeSort;
    }

    @Override
    public String toString() {
        return "TableKey{" +
                "tableName='" + tableName + '\'' +
                ", keyName='" + keyName + '\'' +
                ", keyType='" + keyType + '\'' +
                ", columnName='" + columnName + '\'' +
                ", columnExpression='" + columnExpression + '\'' +
                ", columnPosition=" + columnPosition +
                ", keyTypeSort=" + keyTypeSort +
                '}';
    }

    public void convertColumnExpression(String columnExpression) {
        if (StrUtil.isEmpty(columnExpression)) {
            return;
        }

        // 如果包含双引号【因为函数索引的格式为：NVL("JKOORDERSTATUS",1)】，则取双引号中的内容
        if (columnExpression.contains("\"")) {
            this.columnName = StrUtil.subBetween(columnExpression, "\"","\"");
        }
    }

    /**
     * 处理主键和索引 的脚本拼接
     * @return
     */
    public String toMysqlScript(){
        StringBuilder sb = new StringBuilder();
        // 主键
        if(StrUtil.equalsIgnoreCase(this.keyType, "PRIMARY")){
            sb.append("PRIMARY KEY " + this.keyName + "(" + this.columnName + ") USING BTREE");
        }
        // 唯一索引
        else if(StrUtil.equalsIgnoreCase(this.keyType, "UNIQUE")){
            sb.append("UNIQUE KEY " + this.keyName + "(" + this.columnName + ") USING BTREE");
        }
        // 普通索引
        else {
            sb.append("KEY " + this.keyName + "(" + this.columnName + ") USING BTREE");
        }
        return sb.toString();
    }

}
